//旧版 redux 的使用
//1. 安装 npm i redux
//2. 导入
import {createStore} from 'redux';

//3. 创建 store 对象     store   reducer   action  同步 {type: 'add', payload: 10} 异步 dispatch => {}
let store = createStore((state=0, action) => {
  //根据 action 的描述, 对状态进行更新
  switch(action.type){
    case 'INCRE':
      return state + action.payload;// 将函数的返回值, 作为状态的新值
    case 'DECRE':

    case 'INIT':

    default:
      return state;
  }
})

//4. 分发任务
store.dispatch({type: 'INCRE', payload: 100});
store.dispatch({type: 'DECRE', payload: 100});

// 动作 创造器
function actionCreator(payload){
  return {type: 'INCRE', payload: payload}
}

store.dispatch(actionCreator(100));

//分发异步任务
store.dispatch(dispatch => {
  setTimeout(() => {}, 1000)
  axios.get().then(v => {
    dispatch(actionCreator(100));
  })
})

//声明异步的 action creator.  
function asyncActionCreator(){
  return dispatch => {
    setTimeout(() => {
      dispatch(actionCreator(1000))
    }, 1000)
  }
}

//获取 redux 中的状态
console.log(store.getState());